﻿<?php

/**
 * 信息查询
 *
 * @param       string      act      	  查询的类别(警力查询、出警查询、案件查询)
 * @param       string      By     		 某项查询中的小类别(按车牌号、警号)
 * @param       string      word        查询条件（用户输入的查询信息）
 * @return      array
 */

if (isset ( $_POST ['act'] ) || ! empty ( $_POST ['act'] ) || isset ( $_POST ['word'] ) || ! empty ( $_POST ['word'] )) {
	include_once "../DB_Oracle.php";
	include_once "../JSON.php";
	$DBsql = new DB_Oracle ();
	$act = $_POST ['act'];
	//警力查询
	if ($act == "jl") {
		$control=$_POST['control'];
		if($control=='liandong'){
			$sql_for_area = "select * from dep_area";
			$AreaArr	  = $DBsql->fetch_all_array($sql_for_area);
			//遍历所有区
			$ArrStr = '';
			foreach($AreaArr as $area){
				//根据区id查找该区下的所有单位
				$sql_for_department = "select * from dep_department where AREA_INDEX = '".$area['AREA_INDEX']."'";
				$DepartmentArr		= $DBsql->fetch_all_array($sql_for_department);
				$DepartmentStr 		= '';
				foreach($DepartmentArr as $department){
					$DepartmentStr .=  $department['DEPARTMENT_NAME'].',';
				}
				//所有区的字符串
				$ArrStr .= $area['AREA_NAME'].'#'.$DepartmentStr.'#'.$area['CENTERLNG'].'#'.$area['CENTERLAT'].'|';
			}
			//查询所有警力类型
			$sql_for_type = "select * from dep_vehicle_type";
			$TypeArr	  = $DBsql->fetch_all_array($sql_for_type);
			//遍历警力类型
			$TypeStr = '';
			foreach($TypeArr as $type){
				$TypeStr .= $type['VEHICLE_TYPE_NAME'].',';
			}	
			echo  $ArrStr.'*'.$TypeStr;
		}else if($control=="policeData"){
			$area=$_POST['area'];//所选择的区域
			$units=$_POST['units'];//派出所
			$type=$_POST['type'];//查询的类型
			if($type=="车辆"||$type=="摩托"){
				$sql="select dv.vehicle_index, dv.license_plate
					  from dep_vehicle dv, dep_vehicle_type dvt, dep_department dd
					 where dv.vehicle_type_index = dvt.vehicle_type_index
					   and dv.department_index = dd.department_index
					   and dvt.vehicle_type_name = '".$type."'
					   and dd.department_name = '".$units."'";
				$rows = $DBsql->fetch_all_array ( $sql );
				if(count($rows)!=0){
					echo JSON($rows);
				}else{
					echo '{
						"info":"没有该类型的数据！",
						"status":"y"
					}';
				}
			}else{
				$sql="select dp.personnel_index, dp.personnel_sn
					  from dep_personnel dp, dep_department dd
					 where dp.department_index = dd.department_index
					   and dd.department_name = '".$units."'";
				$rows = $DBsql->fetch_all_array ( $sql );
				if(count($rows)!=0){
					echo JSON($rows);
				}else{
					echo '{
						"info":"没有该类型的数据！",
						"status":"y"
					}';
				}
			}
		}else if($control=="select"){
			$type = $_POST ['By'];
			$word = $_POST ['word'];
			//按车牌号查询
			if ($type == "输入车牌"||$type=="车辆"||$type=="摩托") {
				/*//查询该车辆所在派出所
				$sql = "select dd.misc
					  from dep_vehicle dv, dep_department dd
					 where dv.department_index = dd.department_index
					   and dv.license_plate = '$word'";
				$rowDep = $DBsql->fetch_one_array ( $sql );
				$depId = $rowDep ['MISC'];*/
				//查询出警车辆相关信息
				$sql = "select dd.department_name,
						   dv.license_plate,
						   dvt.vehicle_type_name,
						   da.area_name,
						   dv.sim,
						   trunc(lv.speed,2) speed,
						   trunc(lv.angle,2) angle,
						   trunc(lv.height,2) height,
						   lv.longitude,
						   lv.latitude
					  from dep_vehicle      dv,
						   dep_department   dd,
						   dep_area         da, 
						   dep_vehicle_type dvt,
						   loc_vehicle_0101 lv
					 where dv.department_index = dd.department_index
					   and dv.vehicle_type_index = dvt.vehicle_type_index
					   and dd.area_index=da.area_index
					   and lv.device_id = dv.device_id
					   and dv.license_plate = '$word'
					 order by lv.recv_time desc";
				$rows[] = $DBsql->fetch_one_array ( $sql );
				if($rows[0]!=""){
					echo JSON($rows);
				}else{
					echo '{
						"info":"没有该类型的数据！",
						"status":"y"
					}';
				}
			} else {
				/*//查询该人所在派出所
				$sql = "select dd.misc
					  from dep_Personnel dp, dep_department dd
					 where dp.department_index = dd.department_index
					   and dp.personnel_sn = '$word'";
				$rowDep = $DBsql->fetch_one_array ( $sql );
				$depId = $rowDep ['MISC'];*/
				//查询出警人员相关信息
				$sql = "select dp.personnel_sn,
						   dd.department_name,
						   dp.personnel_name,
						   da.area_name,
						   dp.sim,
						   trunc(lp.speed,2) speed,
						   trunc(lp.angle,2) angle,
						   trunc(lp.height,2) height,
						   dp.personnel_gender,
						   lp.longitude,
						   lp.latitude
					  from dep_Personnel dp, dep_department dd, loc_personnel_0101 lp , dep_area da
					 where dp.department_index = dd.department_index
					   and da.area_index = dd.area_index
					   and lp.device_id = dp.device_id
					   and dp.personnel_sn = '$word'
					 order by lp.recv_time desc";
				$rows[] = $DBsql->fetch_one_array ( $sql );
				if($rows[0]!=""){
					echo JSON($rows);
				}else{
					echo '{
						"info":"没有该类型的数据！",
						"status":"y"
					}';
				}
			}
		}
	} else if ($act == "cj") {
		$src = $_POST ['src'];
		$sdate=$_POST['sdate'];
		$edate=$_POST['edate'];
		if ($src == "one") {
			$sql = "select dm.mission_index,
					       dm.mission_name,
					       dm.mission_member,
					       to_char(mission_start,'yyyy-mm-dd hh24:mi') mission_start,
					       to_char(mission_end,'yyyy-mm-dd hh24:mi') mission_end
					  from dep_mission dm
					 where dm.mission_start >=
					       to_date('$sdate', 'yyyy-mm-dd hh24:mi:ss')
					   and dm.mission_end <=
					       to_date('$edate', 'yyyy-mm-dd hh24:mi:ss')
					 order by dm.mission_start desc";
			$rows = $DBsql->fetch_all_array ( $sql );
			$str = "";
			if (count ( $rows ) != 0) {
				$str .= '<div id="QueryRoadResult" style="height: 150px; overflow-y: scroll;"><div id="message"></div><ul>';
				foreach ( $rows as $k => $row ) {
					if (isset ( $row ['MISSION_MEMBER'] )) {
						$num = explode ( "#", $row ['MISSION_MEMBER'] );
						//出警的车辆信息
						if(!empty($num[0])){
							$sqlCar = "select * from dep_vehicle dv where dv.vehicle_index in (" . $num [0] . ")";
							$comCar = $DBsql->fetch_all_array ( $sqlCar );
							$str .= '<li style="border-top:1px dashed #666">' . $row ['MISSION_NAME'] . ' <br />';
							$carId = 'car' . $k;
							$str .= '车辆轨迹：<select name="car" id="' . $carId . '" onChange="ChangeSrc(\'' . $carId . '\')"><option>请选择车辆</option>';
							foreach ( $comCar as $key => $car ) {
								$str .= '<option value="' . $car ['VEHICLE_INDEX'] . '">' . $car ['LICENSE_PLATE'] . '</option>';
							}
						}
						//出警的人员信息
						if(!empty($num[1])){
							$sqlPer = "select * from dep_personnel dp where dp.personnel_index in (" . $num [1] . ")";
							$comPer = $DBsql->fetch_all_array ( $sqlPer );
							$perId = 'per' . $k;
							$str .= '</select> 人员轨迹：<select name="per" id="' . $perId . '" onChange="ChangeSrc(\'' . $perId . '\')"><option>请选择人员</option>';
							foreach ( $comPer as $key => $per ) {
								$str .= '<option value="' . $per ['PERSONNEL_INDEX'] . '">' . $per ['PERSONNEL_NAME'] . '</option>';
							}
						}
						$str .= '</select><br />时间：<span id="sd'.$k.'">' . $row ['MISSION_START'] . '</span> ～ <span id="ed'.$k.'">' . $row ['MISSION_END'] . '</span></li>';
					}

				}
				$str .= '</ul></div>';
			} else {
				$str .= '暂无数据';
			}
			echo $str;
		} else if ($src == "two") {
			$type = substr ( $_POST ['type'], 0, 3 ); //警员per和警车car的标志
			$sdate = $_POST ['sdate']; //任务开始时间
			$edate = $_POST ['edate']; //任务结束时间
			$pcId = $_POST ['id'];
			if ($type == "car") { //查询警车的轨迹
				/*//查询该车辆所在派出所
				$sql = "select dd.misc
					  from dep_vehicle dv, dep_department dd
					 where dv.department_index = dd.department_index
					   and dv.vehicle_index = $pcId";
				$rowDep = $DBsql->fetch_one_array ( $sql );
				$depId = $rowDep ['MISC'];*/
				//出警车辆的坐标
				$sql = "select lv.longitude, lv.latitude
						  from dep_vehicle dv, loc_vehicle_0101 lv
						 where dv.device_id = lv.device_id
						   and lv.recv_time >=
						       to_date('$sdate', 'yyyy-mm-dd hh24:mi:ss')
						   and lv.recv_time <=
						       to_date('$edate', 'yyyy-mm-dd hh24:mi:ss')
						   and dv.vehicle_index = $pcId";
				$rows = $DBsql->fetch_all_array ( $sql );
				echo json_encode ( $rows );
			} else if ($type == "per") { //查询警员的轨迹
				//查询该人所在派出所
				/*$sql = "select dd.misc
					  from dep_Personnel dp, dep_department dd
					 where dp.department_index = dd.department_index
					   and dp.personnel_index=$pcId";
				$rowDep = $DBsql->fetch_one_array ( $sql );
				$depId = $rowDep ['MISC'];*/
				//出警警员的坐标
				$sql = "select lp.longitude,lp.latitude
					  from dep_personnel dp, loc_personnel_0101 lp
					 where dp.device_id = lp.device_id
					   and lp.recv_time >=
					       to_date('$sdate', 'yyyy-mm-dd hh24:mi:ss')
					   and lp.recv_time <=
					       to_date('$edate', 'yyyy-mm-dd hh24:mi:ss')
					   and dp.personnel_index = $pcId";
				$rows = $DBsql->fetch_all_array ( $sql );
				echo json_encode ( $rows );
			}
		}
	} else if ($act == "aj") {
		$src = $_POST ['src'];
		if ($src == "all") {
			$sql = "select * from dep_mission dm order by dm.mission_start desc";
			$rows = $DBsql->fetch_all_array ( $sql );
			$str = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td style="border-bottom:1px solid #000;">查询指定案件的信息：</td></tr><tr><td>案件名称：';
			$str .= '<select id="aj" name="aj" onChange="changeName()"><option>请选择案件名称</option>';
			foreach ( $rows as $key => $row ) {
				$str .= '<option value=' . $row ['MISSION_MEMBER'].'#' .$row ['MISSION_INDEX']. '>' . $row ['MISSION_NAME'] . '</option>';
			}
			$str .= '</select></td></tr><tr><td align="left"><div id="Task"></div></td></tr><tr><td align="left"><div id="QueryResultTaskName"></div></td></tr></table>';
			echo $str;

		} else if ($src == "one") {
			//参加此次活动的警员和车辆信息
			$valu = $_POST ['value'];
			$str = '<div id="QueryRoadResult"><div id="message"></div><ul>';
			$num = explode ( "#", $valu );
			//出警的车辆信息
			if(!empty($num [0])){
				$sqlCar = "select * from dep_vehicle dv where dv.vehicle_index in (" . $num [0] . ")";
				$comCar = $DBsql->fetch_all_array ( $sqlCar );
				$str .= '<li style="border-top:1px dashed #666">';
				$str .= '车辆轨迹：<select name="car" id="car" onChange="Change(\'car\')"><option>请选择车辆</option>';
				foreach ( $comCar as $key => $car ) {
					$str .= '<option value="' . $car ['VEHICLE_INDEX'] .'#'.$num [2]. '">' . $car ['LICENSE_PLATE'] . '</option>';
				}
			}
			//出警的人员信息
			if(!empty($num [1])){
				$sqlPer = "select * from dep_personnel dp where dp.personnel_index in (" . $num [1] . ")";
				$comPer = $DBsql->fetch_all_array ( $sqlPer );
				$str .= '</select> 人员轨迹：<select name="per" id="per" onChange="Change(\'per\')"><option>请选择人员</option>';
				foreach ( $comPer as $key => $per ) {
					$str .= '<option value="' . $per ['PERSONNEL_INDEX'] .'#'.$num [2]. '">' . $per ['PERSONNEL_NAME'] . '</option>';
				}
			}
			$str .= '</ul></div>';
			echo $str;
		}else if ($src="two"){
			$num=explode('#',$_POST['id']);
			$ajId=$num[0];
			$Id=$num[1];
			$type=$_POST['type'];
			$sql="select to_char(dm.mission_start, 'yyyy-mm-dd hh24:mi:ss') mission_start,
				        to_char(dm.mission_end, 'yyyy-mm-dd hh24:mi:ss') mission_end
				   from dep_mission dm
				  where dm.mission_index = $Id";
			$row = $DBsql->fetch_one_array ( $sql );
			if($type=="car"){//查询警车的轨迹
				//出警车辆的坐标
				$sql = "select lv.longitude, lv.latitude
						  from dep_vehicle dv, loc_vehicle_0101 lv
						 where dv.device_id = lv.device_id
						   and lv.recv_time >=
						       to_date('".$row['MISSION_START']."', 'yyyy-mm-dd hh24:mi:ss')
						   and lv.recv_time <=
						       to_date('".$row['MISSION_END']."', 'yyyy-mm-dd hh24:mi:ss')
						   and dv.vehicle_index = $ajId";
				$rows = $DBsql->fetch_all_array ( $sql );
				echo json_encode ( $rows );
			}else if($type=="per"){//查询警员的轨迹
				//出警警员的坐标
				$sql = "select lp.longitude,lp.latitude
					  from dep_personnel dp, loc_personnel_0101 lp
					 where dp.device_id = lp.device_id
					   and lp.recv_time >=
					       to_date('".$row['MISSION_START']."', 'yyyy-mm-dd hh24:mi:ss')
					   and lp.recv_time <=
					       to_date('".$row['MISSION_END']."', 'yyyy-mm-dd hh24:mi:ss')
					   and dp.personnel_index = $ajId";
				$rows = $DBsql->fetch_all_array ( $sql );
				echo json_encode ( $rows );
			}
		}
	} else {
		echo "没有该请求";
	}
} else {
	echo 'F';
}

?>